package net.yvesd.cellhunter;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CellSelectionActivity extends ListActivity {

	/**
	 * Used for the activity creation intent to store the cell list
	 */
	public static final String CELL_LIST_KEY = "net.yvesd.cellhunter.CellSelectionActivity.CELL_LIST_KEY";

	/**
	 * Used for the intent who returns the results
	 */
	public static final String SELECTED_CELL_KEY = "net.yvesd.cellhunter.CellSelectionActivity.SELECTED_CELL_KEY";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Bundle bundle = this.getIntent().getExtras();
		String[] cellList = bundle.getStringArray(CELL_LIST_KEY);

		setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
				cellList));

		ListView lv = getListView();
		lv.setTextFilterEnabled(true);

		final Intent intent = getIntent();
		// FIXME piggy

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				// When clicked, show a toast with the TextView text
				// Toast.makeText(getApplicationContext(),
				// ((TextView) view).getText(), Toast.LENGTH_SHORT).show();

				StringBuffer sb = new StringBuffer(((TextView) view).getText());

				setResult(RESULT_OK);

				intent.putExtra(SELECTED_CELL_KEY, sb.toString());
				setResult(RESULT_OK, intent);
				finish();
			}
		});

	}

	@Override
	public void onBackPressed() {
		setResult(RESULT_CANCELED);
		super.onBackPressed();
	}
}
